﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext.Net;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for Util
/// </summary>
public class Util
{
    public static int pageSize = 10;
    public static void ParseTime(double runtime, out int hour, out int minute, out int second)
    {
        //double runtime = entity.Runtime;
        hour = (int)runtime / (60 * 60);

        runtime = runtime - hour * 60 * 60;
        minute = (int)runtime / 60;

        runtime = runtime - minute * 60;
        second = (int)runtime;
    }

    public static string GetImageUrl(object imgUrl)
    {
        string value = imgUrl.ToString().Trim();
        if (value == null) return "/Theme/Images/no-image.jpg";
        if (value == "")
        {
            return "/Theme/Images/no-image.jpg";
        }
        if (value.StartsWith("http"))
        {
            return value;
        }
        return "/Files/Films/" + value;
    }

    /// <summary>
    /// Lấy tền file D:\\kdjfkdfj\dkfj\abc.doc
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns>abc.doc</returns>
    public static string GetFileName(string fileName)
    {
        if (fileName.Contains(@"\"))
        {
            try
            {
                return fileName.Substring(fileName.LastIndexOf(@"\") + 1);
            }
            catch { }
        }
        return fileName;
    }

    //Hàm lưu file
    /// <summary>
    /// Hàm lưu file có kiểm tra trùng
    /// </summary>
    /// <param name="fu"></param>
    /// <param name="saveTo">Thư mục chứa file kết thúc phải là dấu \</param>
    /// <returns></returns>
    public static string SaveFileWithoutOverwrite(HttpPostedFile fu, string saveTo)
    {
        saveTo += @"\";
        int count = 1;
        string fileName = fu.FileName;
        //Chuan hoa ten file 
        //Không có dấu cách(thay bằng _)
        fileName = fileName.Replace(" ", "_");
        //Khong dai qua 30 ki tu
        if (fileName.Length > 60)
        {
            fileName = fileName.Substring(fileName.Length - 60);
        }
        // tieng viet khong dau
        fileName = ParseVietToEngChar(fileName);
        //bo nhung ki tu dac biet
        fileName = RemoveSpecCharacter(fileName);

        string[] fileNameSplit = fileName.Split(new char[] { '.' });
        string ext = "." + fileNameSplit[fileNameSplit.Count() - 1];
        string prefix = fileName.Substring(0, fileName.Length - ext.Length);
        while (System.IO.File.Exists(saveTo + fileName))
        {
            fileName = prefix + "[" + count.ToString() + "]" + ext;
            count++;
        }
        fu.SaveAs(saveTo + fileName);
        return fileName;
    }

    #region Common
    // **********************************************************************
    // Convert vietnamese character to english character
    // **********************************************************************
    /// <summary>
    /// ham thay doi mot chuoi tieng viet thanh chuoi khong dau
    /// </summary>
    /// <param name="chucodau">van muon </param>
    /// <returns></returns>
    public static string ParseVietToEngChar(string chucodau)
    {
        string[] pattern = new string[14];

        pattern[0] = "a|(á|ả|à|ạ|ã|ă|ắ|ẳ|ằ|ặ|ẵ|â|ấ|ẩ|ầ|ậ|ẫ)";
        pattern[1] = "o|(ó|ỏ|ò|ọ|õ|ô|ố|ổ|ồ|ộ|ỗ|ơ|ớ|ở|ờ|ợ|ỡ)";
        pattern[2] = "e|(é|è|ẻ|ẹ|ẽ|ê|ế|ề|ể|ệ|ễ)";
        pattern[3] = "u|(ú|ù|ủ|ụ|ũ|ư|ứ|ừ|ử|ự|ữ)";
        pattern[4] = "i|(í|ì|ỉ|ị|ĩ)";
        pattern[5] = "y|(ý|ỳ|ỷ|ỵ|ỹ)";
        pattern[6] = "d|đ";

        pattern[7] = "A|(Á|Ả|À|Ạ|Ã|Ă|Ắ|Ẳ|Ằ|Ặ|Ẵ|Â|Ấ|Ẩ|Ầ|Ậ|Ẫ)";
        pattern[8] = "O|(Ó|Ỏ|Ò|Ọ|Õ|Ô|Ố|Ổ|Ồ|Ộ|Ỗ|Ơ|Ớ|Ở|Ờ|Ợ|Ỡ)";
        pattern[9] = "E|(É|È|Ẻ|Ẹ|Ẽ|Ê|Ế|Ề|Ể|Ệ|Ễ)";
        pattern[10] = "U|(Ú|Ù|Ủ|Ụ|Ũ|Ư|Ứ|Ừ|Ử|Ự|Ữ)";
        pattern[11] = "I|(Í|Ì|Ỉ|Ị|Ĩ)";
        pattern[12] = "Y|(Ý|Ỳ|Ỷ|Ỵ|Ỹ)";
        pattern[13] = "D|Đ";

        for (int i = 0; i < pattern.Length; i++)
        {
            // kí tự sẽ thay thế
            char replaceChar = pattern[i][0];
            MatchCollection matchs = Regex.Matches(chucodau, pattern[i]);

            foreach (Match m in matchs)
            {
                chucodau = chucodau.Replace(m.Value[0], replaceChar);
            }
        }

        return chucodau;
    }

    // **********************************************************************
    // Convert vietnamese character to english character
    // **********************************************************************
    public static string RemoveSpecCharacter(string strSau)
    {
        const string FindText = "+\"<>-`~!@#$%^&*()/?,'\\ |";
        const string ReplText = "_";
        int index = -1;
        char[] arrChar = FindText.ToCharArray();
        while ((index = strSau.IndexOfAny(arrChar)) != -1)
        {
            int index2 = FindText.IndexOf(strSau[index]);
            strSau = strSau.Replace(strSau[index], ReplText[0]);
        }
        return strSau;
    }
    // **********************************************************************
    #endregion Common

    #region Notification

    public static void THONG_BAO_CAP_NHAT(string message)
    {
        Ext.Net.Notification.Show(new Ext.Net.NotificationConfig
        {
            Title = "Thông báo",
            AlignCfg = new Ext.Net.NotificationAlignConfig
            {
                ElementAnchor = Ext.Net.AnchorPoint.Center,
                TargetAnchor = Ext.Net.AnchorPoint.Center,
                OffsetX = -20,
                OffsetY = 20
            },
            ShowFx = new Ext.Net.SlideIn { Anchor = Ext.Net.AnchorPoint.BottomRight, Options = new Ext.Net.FxConfig { Easing = Ext.Net.Easing.BounceOut } },
            HideFx = new Ext.Net.Ghost { Anchor = Ext.Net.AnchorPoint.BottomLeft },
            Html = message
        });
    }
    public static void THONG_BAO_CAP_NHAT_THANH_CONG()
    {
        string message = "Cập nhật thành công";
        Ext.Net.Notification.Show(new Ext.Net.NotificationConfig
        {
            Title = "Thống báo",
            AlignCfg = new Ext.Net.NotificationAlignConfig
            {
                ElementAnchor = Ext.Net.AnchorPoint.BottomRight,
                TargetAnchor = Ext.Net.AnchorPoint.BottomRight,
                OffsetX = -20,
                OffsetY = 20
            },
            ShowFx = new Ext.Net.SlideIn { Anchor = Ext.Net.AnchorPoint.BottomRight, Options = new Ext.Net.FxConfig { Easing = Ext.Net.Easing.BounceOut } },
            HideFx = new Ext.Net.Ghost { Anchor = Ext.Net.AnchorPoint.BottomLeft },
            Html = message
        });
    }


    #endregion Notification
}